1100A - Roman and Browser - CodeForces Solution


implementation *1000

Please click on ads to support us..

Python Code:

n,k = map(int,input().strip().split())
tabs =list(map(int,input().strip().split()))

z=0
ones=0
for i in tabs:
    if i == 1:
        ones+=1
    else: 
        z+=1
        
ans=0
visited=[0]*n
for i in range(len(tabs)):
        if visited[i] == 1:
        continue
    zz=z
    oo = ones
    for j in range(i,len(tabs),k):
        visited[j]=1
        if tabs[j] == 1:
            oo-=1
        else:
            zz-=1
    
        ans = max(ans,abs(zz-oo))
    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long int


void testcase()
{
    int n , k , allpos = 0 , allneg = 0 , plusclos = 0 , negclos = 0 , answer = 0; cin >> n >> k;
    vector<int> v(n);
    for(auto &i : v)
    {
        cin >> i;
        i == 1 ? allpos++ : allneg++;
    }

    for(int b = 0 ; b < n ; b++)
    {
        for(int i = 0 ; i < n ; i++)
        {
            if((i - b) % k == 0)
            {
                v[i] == 1 ? plusclos++ : negclos++;
            } 
        }
        plusclos = allpos - plusclos;
        negclos = allneg - negclos;
        answer = max(answer , abs(plusclos - negclos));
        plusclos = 0 , negclos = 0;
    }

    cout << answer << endl;
}

int32_t main() 
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    clock_t z = clock();

    int tc = 1;
    // cin >> tc;
    while (tc--) testcase();

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC) << endl;
    return 0;
}
    


Comments

Submit
0 Comments
More Questions

1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers